package com.sk.bilibili.model.activity;

import android.text.SpannableStringBuilder;
import android.widget.Toast;

import com.sk.api.annotations.Autowired;
import com.sk.bilibili.bean.Member;
import com.sk.bilibili.callBack.RequestCallBack;
import com.sk.bilibili.mapper.MemberMapper;
import com.sk.bilibili.mapper.SmsMapper;
import com.sk.bilibili.model.BaseModel;
import com.sk.bilibili.other.JsonMap;
import com.sk.bilibili.utils.DataUtils;
import com.sk.bilibili.utils.PreferenceUtils;
import com.sk.bilibili.utils.RequestUtils;
import com.sk.bilibili.utils.StringUtils;
import com.sk.bilibili.view.activity.RegisterActivity;

public class RegisterModel extends BaseModel<RegisterActivity> {


    public SpannableStringBuilder getText(int color) {
        return StringUtils.getColorText("登录即代表你同意用户协议和隐式政策", color, 8, 12, 13, 17);
    }

    @Autowired
    SmsMapper smsMapper;
    @Autowired
    MemberMapper memberMapper;
    @Autowired
    PreferenceUtils preferenceUtils;

    public void sendMsg(String phone) {
        RequestUtils.process(smsMapper.sendMsg(phone), new RequestCallBack<Void>() {
            @Override
            public void success(Void unused) {
                view.sendSuccess();
            }
            @Override
            public void error(Throwable throwable) {
                view.sendError(throwable.getMessage());
            }
        });
    }

    public void registerAndLogin(String phone, String password, String code) {
        JsonMap map=new JsonMap();
        map.put("code",code);
        map.put("member.phone",phone);
        map.put("member.password",password);
        map.put("member.nickName","新用户");
        RequestUtils.process(memberMapper.register(map), new RequestCallBack<Member>() {
            @Override
            public void success(Member member) {
                RequestUtils.process(memberMapper.login(map), new RequestCallBack<Member>() {
                    @Override
                    public void success(Member member) {
                        preferenceUtils.setString("bSessionId",member.getExtra().getBSessionId());
                        DataUtils.setMemberId(member.getId());
                        view.loginSuccess();
                    }
                    @Override
                    public void error(Throwable throwable) {
                        view.loginError(throwable.getMessage());
                    }
                });
            }
            @Override
            public void error(Throwable throwable) {
                view.registerError(throwable.getMessage());
            }
        });
    }

}